#!/usr/bin/env python
# -*- coding: utf-8 -*-

import json
import time
from Atm.core import accounts

def get_bill_date(acc_data):
    current_time = time.localtime(time.time())
    current_year = current_time.tm_year
    current_month = current_time.tm_mon
    repay_day = time.mktime(time.strptime('%s-%s-%s' % (current_year, current_month, acc_data['account_data']['repay_day']), "%Y-%m-%d"))
    pay_day = time.mktime(time.strptime('%s-%s-%s' % (current_year, current_month, acc_data['account_data']['pay_day']), "%Y-%m-%d"))
    start_pay_day = time.mktime(time.strptime('%s-%s-%s' % (current_year, current_month-1, acc_data['account_data']['pay_day']), "%Y-%m-%d"))
    return {"repay_day":repay_day,"pay_day":pay_day,"start_pay_day":start_pay_day}

def current_bill_amount(acc_data):
    '''
    查询已出账单
    :param acc_data:
    :return:
    '''
    bill_all_date = get_bill_date(acc_data)
    consume_amount = 0
    interest = 0
    account_bill = accounts.load_bill(acc_data['account_id'])
    if acc_data['account_data']['credit'] > acc_data['account_data']['balance']:
        if time.time() > bill_all_date['pay_day'] and time.time() < bill_all_date['repay_day']:
            if len(account_bill) > 0:
                for item in account_bill:
                    item = json.loads(item)
                    consume_date = time.mktime(time.strptime(item["consume_date"], "%Y-%m-%d"))
                    if consume_date > bill_all_date['start_pay_day'] and consume_date < bill_all_date['pay_day']:
                        consume_amount += item['consume_amount']
                        interest += item['interest']
                if (acc_data['account_data']['balance'] + consume_amount) < acc_data['account_data']['credit']:
                    print('''             ========== %s 账单详情 ==========
                    本期账单金额: ￥%s
                    当前余额:    ￥%s''' % (acc_data['account_id'], consume_amount + interest, acc_data['account_data']['balance']))
                else:
                    print('本期账单已还清...')
    else:
        print('本期账单已还清...')

def current_bill_detail(acc_data):
    '''
    查询已出账单明细
    :param acc_data:
    :return:
    '''
    flag = False
    bill_all_date = get_bill_date(acc_data)
    account_bill = accounts.load_bill(acc_data['account_id'])
    if time.time() > bill_all_date['pay_day'] and time.time() < bill_all_date['repay_day']:
        if len(account_bill) > 0:
            for item in account_bill:
                item = json.loads(item)
                consume_date = time.mktime(time.strptime(item["consume_date"], "%Y-%m-%d"))
                if consume_date > bill_all_date['start_pay_day'] and consume_date < bill_all_date['pay_day']:
                    print('''账户: %s 日期: %s 用途: %s 金额: ￥%s
                    ''' % (item['id'], item['consume_date'], item['consume_type'], item['consume_amount']+item['interest']))
                else:
                    flag = True
            if flag:
                print('本期没有账单明细...')
                flag = False

def not_bill(acc_data):
    '''
    查询未出账单金额
    :param acc_data:
    :return:
    '''
    bill_all_date = get_bill_date(acc_data)
    consume_amount = 0
    interest = 0
    rem_amount = 0
    account_bill = accounts.load_bill(acc_data['account_id'])
    if acc_data['account_data']['credit'] > acc_data['account_data']['balance']:
        if time.time() > bill_all_date['pay_day']:
            if len(account_bill) > 0:
                for item in account_bill:
                    item = json.loads(item)
                    consume_amount += item['consume_amount']
                    interest += item['interest']
                    consume_date = time.mktime(time.strptime(item["consume_date"], "%Y-%m-%d"))
                    if bill_all_date['pay_day'] < consume_date:
                        if (consume_amount + interest + acc_data['account_data']['balance']) > acc_data['account_data']['credit']:
                            rem_amount = acc_data['account_data']['credit'] - (consume_amount + interest + acc_data['account_data']['balance'])
                            rem_amount += consume_amount + interest
                        else:
                            rem_amount = consume_amount + interest
                print('''          ========= %s 未出账单详情 =========
                未出账单:    ￥%s
                当前余额:    ￥%s''' % (acc_data['account_id'], rem_amount, acc_data['account_data']['balance']))

def not_bill_detail(acc_data):
    '''
    查询未出账单明细
    :param acc_data:
    :return:
    '''
    flag = False
    bill_all_date = get_bill_date(acc_data)
    account_bill = accounts.load_bill(acc_data['account_id'])
    if time.time() > bill_all_date['pay_day']:
        if len(account_bill) > 0:
            for item in account_bill:
                item = json.loads(item)
                consume_date = time.mktime(time.strptime(item["consume_date"], "%Y-%m-%d"))
                if time.time() > consume_date and consume_date > bill_all_date['pay_day']:
                    print('''账户: %s 日期: %s 用途: %s 金额: ￥%s
                    ''' % (item['id'], item['consume_date'], item['consume_type'], item['consume_amount']+item['interest']))
                else:
                    flag = True
            if flag:
                print('未出账单明细为空...')

def bill_check(acc_data):
    '''
    查询账单函数
    :param acc_data:
    :return:
    '''
    menu = '''---------- 账单查询 ----------
    1. 查询本期应还金额
    2. 查询本期账单明细
    3. 查询未出账单金额
    4. 查询未出账单明细
    5. 退出账单查询
    '''
    menu_dic = {
        '1': current_bill_amount,
        '2': current_bill_detail,
        '3': not_bill,
        '4': not_bill_detail
    }
    while True:
        print(menu)
        choose = input('请选择操作:').strip()
        if choose:continue
        if choose == '5':break
        if choose in menu_dic:
            menu_dic[choose](acc_data)
        else:
            print('你输入的选项不存在...')